/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.handwritingtime.service;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.handwritingtime.entity.HandwritingTime;
import com.jeeplus.modules.handwritingtime.mapper.HandwritingTimeMapper;
import com.jeeplus.modules.handwritingtime.entity.HandwritingAttachedTime;
import com.jeeplus.modules.handwritingtime.mapper.HandwritingAttachedTimeMapper;

/**
 * 手写签名时间管理Service
 * @author 张鑫
 * @version 2022-06-01
 */
@Service
@Transactional(readOnly = true)
public class HandwritingTimeService extends CrudService<HandwritingTimeMapper, HandwritingTime> {

	@Autowired
	HandwritingTimeMapper mapper;
	@Autowired
	HandwritingAttachedTimeMapper handwritingAttachedTimeMapper;

	public boolean findTimeZone() {
		HandwritingTime handwritingTime = mapper.findTimeZone();
		if (handwritingTime == null){
			return false;
		}
		Date current = new Date();
		Date startTime = handwritingTime.getStartTime();
		Date endTime = handwritingTime.getEndTime();
		if (belongCalendar(current, startTime, endTime)) {
			return true;
		} else {
			return false;
		}
	}

	public boolean findAttachedTimeZone() {
		// 获取大时间
		HandwritingTime handwritingTime = mapper.findTimeZone();
		if (handwritingTime == null){
			return false;
		}
		// 获取小时间
		List<HandwritingAttachedTime> list = handwritingAttachedTimeMapper.find();
		if (list.size() <= 0){
			return false;
		}
		Date current = new Date();
		Date startTime = handwritingTime.getStartTime();
		Date endTime = handwritingTime.getEndTime();

		Boolean flag = true;
		// 具体的判断
		if(belongCalendar(current,startTime,endTime)) {
			for (HandwritingAttachedTime temp : list) {
				// 如果有一个不符合条件，让flag = false，并且停止循环。返回false
				Date startTimePause = temp.getStartTime();
				Date endTimePause = temp.getEndTime();
				//在暂停时间区间之前
				if ((current.compareTo(startTimePause) < 0 && current.compareTo(endTimePause) < 0) || (current.compareTo(startTimePause) > 0 && current.compareTo(endTimePause) > 0)) {  //在暂停时间区间之后
					//处于评价时间，非暂停时间
				} else {
					//处于评价时间，暂停时间
					flag = false;
					break;
				}
			}
			//处于非评价时间
		}else {
			flag = false;
		}
		return flag;
	}

	private boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(beginTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		return date.after(begin) && date.before(end);
	}

	public HandwritingTime get(String id) {
		HandwritingTime handwritingTime = super.get(id);
		handwritingTime.setHandwritingAttachedTimeList(handwritingAttachedTimeMapper.findList(new HandwritingAttachedTime(handwritingTime)));
		return handwritingTime;
	}

	public List<HandwritingTime> findList(HandwritingTime handwritingTime) {
		return super.findList(handwritingTime);
	}

	public Page<HandwritingTime> findPage(Page<HandwritingTime> page, HandwritingTime handwritingTime) {
		return super.findPage(page, handwritingTime);
	}

	public Page<HandwritingTime> findAllPage(Page<HandwritingTime> page, HandwritingTime handwritingTime) {
		dataRuleFilter(handwritingTime);
		handwritingTime.setPage(page);
		page.setList(mapper.findAllList(handwritingTime));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(HandwritingTime handwritingTime) {
		super.save(handwritingTime);
		for (HandwritingAttachedTime handwritingAttachedTime : handwritingTime.getHandwritingAttachedTimeList()){
			if (handwritingAttachedTime.getId() == null){
				continue;
			}
			if (HandwritingAttachedTime.DEL_FLAG_NORMAL.equals(handwritingAttachedTime.getDelFlag())){
				if (StringUtils.isBlank(handwritingAttachedTime.getId())){
				    handwritingAttachedTime.setHandwritingTime(handwritingTime);
					handwritingAttachedTime.preInsert();
					handwritingAttachedTimeMapper.insert(handwritingAttachedTime);
				}else{
					handwritingAttachedTime.preUpdate();
					handwritingAttachedTimeMapper.update(handwritingAttachedTime);
				}
			}else{
//				物理删除
//				handwritingAttachedTimeMapper.delete(handwritingAttachedTime);
//				逻辑删除
				handwritingAttachedTimeMapper.deleteByLogic(handwritingAttachedTime);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(HandwritingTime handwritingTime) {
		super.delete(handwritingTime);
		handwritingAttachedTimeMapper.delete(new HandwritingAttachedTime(handwritingTime));
	}

	@Transactional(readOnly = false)
	public void deleteByLogic(HandwritingTime handwritingTime) {
		super.deleteByLogic(handwritingTime);
		handwritingAttachedTimeMapper.deleteByLogic(new HandwritingAttachedTime(handwritingTime));
	}

}